package com.phone580.app.download;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.phone580.app.Constant;
import com.phone580.app.MainApplication;
import com.phone580.app.R;
import com.phone580.app.entity.DownloadInfoEntity;
import com.phone580.app.untilty.CommonUtility;

public class UnFinishDownloadAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<DownloadInfoEntity> data;
	private Context context;
	private MainApplication app;
	private Handler handler;
	
	public UnFinishDownloadAdapter(Context context) {
		this.context = context;
		inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		app = (MainApplication) context.getApplicationContext();
		refresh();
	}
	
	public void setHandler(Handler handler) {
		this.handler = handler;
	}
	
	public void refresh() {
		if (data == null) {
			data = new ArrayList<DownloadInfoEntity>();
			return;
		}
		data.clear();
		List<DownloadInfoEntity> list = app.getDownloadList();
		for(DownloadInfoEntity info : list) {
			if (info.getStatus() != Constant.DOWNLOAD_FINISH && info.getStatus() != Constant.DOWNLOAD_CANCEL) {
				data.add(info);
			}
		}
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public DownloadInfoEntity getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			v = inflater.inflate(R.layout.download_manager_item, null);
			ViewHolder holder = new ViewHolder();
			holder.tvName = (TextView) v.findViewById(R.id.download_manager_item_name_tv);
			holder.pbProgress = (ProgressBar) v.findViewById(R.id.download_manager_item_pb);
			holder.btnStart = (TextView) v.findViewById(R.id.download_manager_item_start_tv);
			holder.btnPause = (TextView) v.findViewById(R.id.download_manager_item_pause_tv);
			holder.btnCancel = (TextView) v.findViewById(R.id.download_manager_item_cancel_tv);
			holder.ivImage = (ImageView) v.findViewById(R.id.unfinish_down_item_icon_tv);
			v.setTag(holder);
		}
		final ViewHolder holder = (ViewHolder) v.getTag();
		final DownloadInfoEntity entity = getItem(position);
		Bitmap bm = CommonUtility.getIcon(context, entity.getIcon());
		holder.ivImage.setImageBitmap(bm);
		holder.tvName.setText(entity.getFileName());
		holder.pbProgress.setMax(entity.getFileSize());
		holder.pbProgress.setProgress(entity.getCompleteSize());
		if (entity.getStatus() == Constant.DOWNLOAD_STANDBY) {
			holder.btnStart.setVisibility(View.GONE);
			holder.btnPause.setVisibility(View.VISIBLE);
		}
		if (entity.getStatus() == Constant.DOWNLOAD_START) {
			holder.btnStart.setVisibility(View.GONE);
			holder.btnPause.setVisibility(View.VISIBLE);
		}
		if (entity.getStatus() == Constant.DOWNLOAD_PAUSE) {
			holder.btnStart.setVisibility(View.VISIBLE);
			holder.btnPause.setVisibility(View.GONE);
		}
		holder.btnStart.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				holder.btnStart.setVisibility(View.GONE);
				holder.btnPause.setVisibility(View.VISIBLE);
//				((MainApplication)context.getApplicationContext()).callback.startDownload(entity);
				((MainApplication)context.getApplicationContext()).startDownload(entity);
			}
		});
		holder.btnStart.setTag(entity.getUrl());
		
		holder.btnPause.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				holder.btnStart.setVisibility(View.VISIBLE);
				holder.btnPause.setVisibility(View.GONE);
//				((MainApplication)context.getApplicationContext()).callback.pauseDownload(entity);
				((MainApplication)context.getApplicationContext()).pauseDownload(entity);
			}
		});
		holder.btnPause.setTag(entity.getUrl());
		
		holder.btnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
//				v.setEnabled(false);
//				if (data.contains(entity)) {
//					data.remove(entity);
//					notifyDataSetChanged();
//				}
				handler.sendEmptyMessage(1);
				((MainApplication)context.getApplicationContext()).cancelDownload(entity);
				System.out.println(entity.getFileName() + "----------cancel");
				handler.sendEmptyMessage(2);
//				notifyDataSetChanged();
			}
		});
		holder.btnCancel.setTag(entity.getUrl());
		
		holder.pbProgress.setMax(entity.getFileSize());
		holder.pbProgress.setProgress(entity.getCompleteSize());
		return v;
	}
	
	public class ViewHolder {
		public TextView tvName;
		public ProgressBar pbProgress;
		public TextView btnStart;
		public TextView btnPause;
		public TextView btnCancel;
		public ImageView ivImage;
	}

}
